import numpy as np
import matplotlib.pyplot as plt



## Charge ponctuelle

# Définition des paramètres
epsilon0 = 8.854e-12
L = 1e-9
N = 100
q = 1.6e-19
# Création des matrices X[i,j] et Y[i,j]
axeX = np.linspace(-L/2,L/2,N)
axeY = np.linspace(-L/2,L/2,N)
(X,Y) = np.meshgrid(axeX,axeY)
# Création de la matrice V[i,j]
V = np.zeros((N,N))
# Calcul du potentiel :
for i in range(N):
    for j in range(N):
        AM = ( (X[i,j]-0)**2 + (Y[i,j]-0)**2 )**(1/2)
        V[i,j] = q/(4*np.pi*epsilon0*AM)
# Calcul des champs :
(Ey,Ex) = np.gradient(-V)
# Tracé des surfaces équipotentielles et des lignes de champ :
plt.figure()
plt.contour(X,Y,V,100)
plt.streamplot(X,Y,Ex,Ey)
plt.show()



## Molécule de chlorure d'hydrogène

# Définition des paramètres
epsilon0 = 8.854e-12
L = 1e-9
N = 100
deltaq = 2.9e-20
a = 127e-12
# Création des matrices X[i,j] et Y[i,j]
axeX = np.linspace(-L/2,L/2,N)
axeY = np.linspace(-L/2,L/2,N)
(X,Y) = np.meshgrid(axeX,axeY)
# Création de la matrice V[i,j]
V = np.zeros((N,N))
# Calcul du potentiel :
for i in range(N):
    for j in range(N):
        AM_H = ( (X[i,j]-0)**2 + (Y[i,j]-a/2)**2 )**(1/2)
        AM_Cl = ( (X[i,j]-0)**2 + (Y[i,j]+a/2)**2 )**(1/2)
        V[i,j] = deltaq/(4*np.pi*epsilon0*AM_H) - deltaq/(4*np.pi*epsilon0*AM_Cl)
# Calcul des champs :
(Ey,Ex) = np.gradient(-V)
# Tracé des surfaces équipotentielles et des lignes de champ :
plt.figure()
plt.contour(X,Y,V,100)
plt.streamplot(X,Y,Ex,Ey)
plt.show()



## Condensateur plan

# Définition des paramètres
epsilon0 = 8.854e-12
L = 1e-2
N = 100
l = 7e-3
d = 3e-3
Q = 1e-13
# Création des matrices X[i,j] et Y[i,j]
axeX = np.linspace(-L/2,L/2,N)
axeY = np.linspace(-L/2,L/2,N)
(X,Y) = np.meshgrid(axeX,axeY)
# Création de la matrice V[i,j]
V = np.zeros((N,N))
# Calcul du potentiel :
for i in range(N):
    for j in range(N):
        for k in range(20): # charges positives réparties sur l'armature supérieure
            x_k=-l/2+l/19*k # abscisse de la charge k
            AM_k = ( (X[i,j]-x_k)**2 + (Y[i,j]-d/2)**2 )**(1/2)
            V[i,j] = V[i,j] + (Q/20)/(4*np.pi*epsilon0*AM_k)
        for k in range(20): # charges négatives réparties sur l'armature inférieure
            x_k=-l/2+l/19*k # abscisse de la charge k
            AM_k = ( (X[i,j]-x_k)**2 + (Y[i,j]+d/2)**2 )**(1/2)
            V[i,j] = V[i,j] + (-Q/20)/(4*np.pi*epsilon0*AM_k)
# Calcul des champs :
(Ey,Ex) = np.gradient(-V)
# Tracé des surfaces équipotentielles et des lignes de champ :
plt.figure()
plt.contour(X,Y,V,100)
plt.streamplot(X,Y,Ex,Ey)
plt.show()



